package vn.fsoft.gst.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import vn.fsoft.gst.domain.Department;
import vn.fsoft.gst.domain.Directorate;
import vn.fsoft.gst.service.AddressService;
import vn.fsoft.gst.service.ContactService;
import vn.fsoft.gst.service.DepartmentService;
import vn.fsoft.gst.service.DirectorateService;
import vn.fsoft.gst.service.OrganisationService;
import vn.fsoft.gst.service.SicService;

/**
 * 
 * @author Tung
 *
 */

@Controller
public class DepartmentController {
	
	@Autowired
    private DepartmentService departmentService;
	
	@Autowired
	private DirectorateService directorateService;
	
	@Autowired
	private OrganisationService organisationService;
	
	@Autowired
	private ContactService contactService;
	
	@Autowired
	private AddressService addressService;
	
	@Autowired
	private SicService sicService;

	@RequestMapping(value = "/adddepartment", method = RequestMethod.GET)
	public String addDepartment( @RequestParam(value = "direcId", required = false) String direcId, 
			@RequestParam(value="departId",required = false) String departId, Model model) {
		Directorate directorate = null;
		Department department = null;
		String typeBussiness = null;
		
		if (direcId != null) {                 //add a new department
			directorate = directorateService.getDirectorateById(Integer.parseInt(direcId));
			department = new Department();
		} else {                               //update a old department
			department = departmentService.getDepartmentById(Integer.parseInt(departId));
			typeBussiness = sicService.getTypeOfBussiness(department.getSicCode());
			directorate = department.getDirectorate();
		}
		
		model.addAttribute("directorate", directorate);
		model.addAttribute("department", department);
		model.addAttribute("typeBussiness", typeBussiness);
		
		return "addDepartment";
	}
	
	@RequestMapping(value = "/adddepartment", method = RequestMethod.POST)
    public String saveDepartment(@ModelAttribute("department")Department department,
    		@RequestParam int direcId) {	
		Directorate directorate = directorateService.getDirectorateById(direcId);
	
		department.setDirectorate(directorate);
		departmentService.addDepartment(department);
		
		return "redirect:amendDirectorate?directorateID=" + direcId
				+ "&orgID=" + directorate.getOrganisation().getOrgID();
    }
	
	@RequestMapping(value = "/updatedepartment", method = RequestMethod.POST)
	public String updateDepartment(@ModelAttribute("department")Department department,
			@RequestParam int direcId) {
		Directorate directorate = directorateService.getDirectorateById(direcId);
	
		department.setDirectorate(directorate);
		departmentService.editDepartment(department);
		
		return "redirect:amendDirectorate?directorateID=" + direcId
				+ "&orgID=" + directorate.getOrganisation().getOrgID();
	}
	
	@RequestMapping(value="/activedepartment", method=RequestMethod.GET)
	public void activeDepartment(@RequestParam int departId,Model model) {	
	    departmentService.markActiveDepartment(departId);
	}
	
	@RequestMapping(value="/inactivedepartment", method=RequestMethod.GET)
	public void inActiveDepartment(@RequestParam int departId,Model model) {	
	    departmentService.markInActiveDepartment(departId);
	}
	
	@RequestMapping(value="/checkuniquedepartmentname", method=RequestMethod.GET)
	public void checkUniqueDepartmentName(@RequestParam String departmentName,Model model) {	
	    boolean flag = false;
		flag = departmentService.checkUniqueName(departmentName);
	    model.addAttribute("flag", flag);
	}
}
